// 编码//ascii->base64  //(只能是英文数字，不能是汉字等,如果是汉字就报错)
const windowCode = {
  encode: str => window.btoa(str),
  decode: codeStr => window.atob(codeStr),
};

// 通过 Unicode 编码加密
const byUnicode = {
  encode: str => {
    var c = String.fromCharCode(str.charCodeAt(0) + str.length);
    for (var i = 1; i < str.length; i++) {
      c += String.fromCharCode(str.charCodeAt(i) + str.charCodeAt(i - 1));
    }
    return escape(c);
  },
  decode: codeStr => {
    codeStr = unescape(codeStr);
    var c = String.fromCharCode(codeStr.charCodeAt(0) - codeStr.length);
    for (var i = 1; i < codeStr.length; i++) {
      c += String.fromCharCode(codeStr.charCodeAt(i) - c.charCodeAt(i - 1));
    }
    return c;
  },
};


// 组合加密
const encode = str => {
  const code = byUnicode.encode(str);
  return windowCode.encode(code);
};

const decode = code => {
  const str = windowCode.decode(code);
  return byUnicode.decode(str);
};

export default { encode, decode };
